package com.xtedu.examproject.dao.impl;

import com.xtedu.examproject.dao.base.TemplateDetailDao;
import com.xtedu.examproject.entity.TemplateDetail;
import com.xtedu.examproject.util.DBUtil;
import com.xtedu.examproject.util.PageHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/*
 * Author: xzy
 * Date: 2025/10/16
 * Last Modified by: xzy
 * Last Modified date: 2025/10/16
 * Description: TODO - Add file description here
 */
public class TemplateDetailDaoImpl implements TemplateDetailDao {

    @Override
    public int insert(TemplateDetail templateDetail) throws Exception {
        String sql = "insert into template_detail (template_id, questiontype_id, question_count, score_per_question) values (?,?,?,?)";
        try (
                Connection conn = DBUtil.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql)
        ) {
            ps.setInt(1, templateDetail.getTemplateId());
            ps.setInt(2, templateDetail.getQuestionTypeId());
            ps.setInt(3, templateDetail.getQuestionCount());
            ps.setInt(4, templateDetail.getScorePerQuestion());
            return ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    @Override
    public int update(TemplateDetail templateDetail) throws Exception {
        String sql = "update template_detail set template_id=?, questiontype_id=?, question_count=?, score_per_question=? where id=?";
        try (
                Connection conn = DBUtil.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql)
        ) {
            ps.setInt(1, templateDetail.getTemplateId());
            ps.setInt(2, templateDetail.getQuestionTypeId());
            ps.setInt(3, templateDetail.getQuestionCount());
            ps.setInt(4, templateDetail.getScorePerQuestion());
            return ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    @Override
    public int delete(TemplateDetail templateDetail) throws Exception {
        String sql = "delete from template_detail where id=?";
        try (
                Connection conn = DBUtil.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql)
        ) {
            ps.setInt(1, templateDetail.getId());
            return ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    @Override
    public int deleteById(int id) throws Exception {
        String sql = "delete from template_detail where id=?";
        try (
                Connection conn = DBUtil.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql)
        ) {
            ps.setInt(1, id);
            return ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    @Override
    public TemplateDetail findById(int id) throws Exception {
        String sql = """
                SELECT td.*, pt.template_name,q.type_name FROM template_detail td
                LEFT JOIN paper_templates pt ON td.template_id = pt.id
                LEFT JOIN questiontype q ON td.questiontype_id = q.id
                WHERE td.id=?
                """;
        try (
                Connection conn = DBUtil.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql)
        ) {
            ps.setInt(1, id);
            ResultSet resultSet = ps.executeQuery();
            TemplateDetail templateDetail = new TemplateDetail();
            if (resultSet.next()) {
                templateDetail.setId(resultSet.getInt("id"));
                templateDetail.setTemplateId(resultSet.getInt("template_id"));
                templateDetail.setQuestionTypeId(resultSet.getInt("questiontype_id"));
                templateDetail.setQuestionCount(resultSet.getInt("question_count"));
                templateDetail.setScorePerQuestion(resultSet.getInt("score_per_question"));
                templateDetail.setTemplateName(resultSet.getString("template_name"));
                templateDetail.setQuestionTypeName(resultSet.getString("type_name"));
            }
            return templateDetail;
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    @Override
    public List<TemplateDetail> findByTemplateId(int templateId) {
        String sql = """
                SELECT td.*, pt.template_name,q.type_name FROM template_detail td
                LEFT JOIN paper_templates pt ON td.template_id = pt.id
                LEFT JOIN questiontype q ON td.questiontype_id = q.id
                WHERE td.template_id=?
                """;
        try (
                Connection conn = DBUtil.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql)
        ) {
            ps.setInt(1, templateId);
            ResultSet resultSet = ps.executeQuery();
            List<TemplateDetail> templateDetails = new ArrayList<>();
            while (resultSet.next()) {
                TemplateDetail templateDetail = new TemplateDetail();
                templateDetail.setId(resultSet.getInt("id"));
                templateDetail.setTemplateId(resultSet.getInt("template_id"));
                templateDetail.setQuestionTypeId(resultSet.getInt("questiontype_id"));
                templateDetail.setQuestionCount(resultSet.getInt("question_count"));
                templateDetail.setScorePerQuestion(resultSet.getInt("score_per_question"));
                templateDetail.setTemplateName(resultSet.getString("template_name"));
                templateDetail.setQuestionTypeName(resultSet.getString("type_name"));
                templateDetails.add(templateDetail);
            }
            return templateDetails;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public List<TemplateDetail> findAll() throws Exception {
        String sql = "select * from template_detail";
        try (
                Connection conn = DBUtil.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql)
        ) {
            ResultSet resultSet = ps.executeQuery();
            List<TemplateDetail> templateDetails = new ArrayList<>();
            while (resultSet.next()) {
                TemplateDetail templateDetail = new TemplateDetail();
                templateDetail.setId(resultSet.getInt("id"));
                templateDetail.setTemplateId(resultSet.getInt("template_id"));
                templateDetail.setQuestionTypeId(resultSet.getInt("questiontype_id"));
                templateDetail.setQuestionCount(resultSet.getInt("question_count"));
                templateDetail.setScorePerQuestion(resultSet.getInt("score_per_question"));
                templateDetail.setTemplateName(resultSet.getString("template_name"));
                templateDetail.setQuestionTypeName(resultSet.getString("type_name"));
                templateDetails.add(templateDetail);
            }
            return templateDetails;
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    @Override
    public PageHelper findAllByPage(int pageindex, int pagesize) throws Exception {
        return null;
    }

    @Override
    public PageHelper findPageByRoleIds(int pageindex, int pagesize, int... roleIds) throws Exception {
        return null;
    }
}
